﻿using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using Surging.Core.Common;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;


namespace Dora.Services.AdminUser.Repository.Repositorys
{
    public class OrgWxMPThemeRepository: SqlSugarRepository<OrgWxMPThemeEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public OrgWxMPThemeRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<OrgWxMPThemeEntity>> GetPageList(OrgWxMPThemeQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<OrgWxMPThemeEntity>()
                .Where(p => p.IsDeleted == 0 && p.ThemeName.Contains(input.ThemeName)&&p.ThemeKey.Contains(input.ThemeKey)&&p.OrgId==input.OrgId);
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<OrgWxMPThemeEntity>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        /// <summary>
        /// 删除广告位
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public new async Task<bool> Delete(OrgWxMPThemeEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable<OrgWxMPThemeEntity>()
                .SetColumns(it => new OrgWxMPThemeEntity() { IsDeleted = 1, UpdatedTime = DateTime.Now, UpdatedUserName = entity.UpdatedUserName, UpdatedUserId = entity.CreatedUserId })
                .Where(it => it.Id == entity.Id).ExecuteCommandAsync() > 0;
            return result;
        }

        public async Task<int> CreateRange(List<OrgWxMPThemeEntity> list)
        {
            var result = await _unitOfWork.ModifyClient.Insertable(list).ExecuteCommandAsync();
            return result;
        }

    }
}
